<?php
namespace app\api\controller;

use app\common\model\AppleDebtBacked;
use app\common\model\AppleRepayPlan;
use app\common\model\OppoOrder;
use app\common\model\SonyOrderRepay;
use app\common\model\SysMerchantWithdraw;
use think\admin\Controller;
use think\admin\extend\CodeExtend;
use think\facade\Db;
use think\facade\Log;

class Callback extends Controller
{

    public function payH5Notify()
    {
        $params = $this->request->request();
        //Log::info($params);

        $orderRepay = SonyOrderRepay::where(['pay_id' => $params['out_trade_no']])->find();
        if(empty($orderRepay)){
            Log::debug($params['out_trade_no'] . "错误回调，无支付记录");
        }
        $plan = AppleRepayPlan::where(['order_id' => $orderRepay['order_id'],'plan_index' => $orderRepay['plan_index']])->find();
        Db::startTrans();
        try {
            //付款单
            $orderRepay->repay_amount = $params['receipt_amount'];
            $orderRepay->status = 1;
            $orderRepay->save();

            $status = 0;
            $remaining_amount = 0;
            //判断是否全部还清
            $amount = $params['receipt_amount'];
            $rent = $plan['plan_total_amount'];
            $jm = $plan['deduction_amount'];

            //还款记录
            //租金
            $backed = [
                'backed_id' => CodeExtend::uniqidDate(18, 'BA'),
                'debt_id' => '',
                'debt_plan_id' => '',
                'bill_id' => '',
                'plan_index' => $plan['plan_index'],
                'order_id' => $plan['order_id'],
                'pay_id' => CodeExtend::uniqidDate(18, 'PA'),
                'biz_type' => 1,
                'member_id' => $plan['member_id'],
                'backed_time' => $params['gmt_payment'],
                'credit_amount' => $plan['deduction_amount'] ?? '0.00',
                'discount_amount' => 0,
                'backed_type' => $plan['repay_type'],
                'memo' => $plan['repay_remark'],
                'tenant_id' => 10000,
                'merchant_id' => $plan['merchant_id'],
                'shop_name' => $plan['shop_name'],
            ];
            if($plan['bill_type'] == 1){
                $bill_type = 1;
                //还款金额 + 减免
                $totalAmount = $amount + $jm;
                if($totalAmount < $rent){
                    //部分还款
                    $payment_amount = $totalAmount;
                    $remaining_amount = $rent - $totalAmount;
                }else{
                    //结清
                    $status = 1;
                    $payment_amount = $amount;
                }

            }else{
                //买断记录
                $bill_type = 5;
                $remaining_amount = 0;
                $payment_amount = $amount;
            }
            $backed['bill_type'] = $bill_type;
            $backed['payment_amount'] = $amount;
            $backed['remaining_amount'] = $remaining_amount;
            $backed['status'] = 1;
            AppleDebtBacked::mk()->insert($backed);

            if($status == 1){
                $plan->status = 1;
                $plan->settle_time = date('Y-m-d H:i:s');
                $plan->save();
            }

            if($plan['plan_index'] == 12 && $plan['bill_type'] == 5){
                $order = OppoOrder::mk()->where(['order_id' => $plan['order_id']])->find();
                $order->buy_sts = 1;
                $order->status = 6;
                $order->save();
            }

            //记录商家收入
            $mWithdraw = SysMerchantWithdraw::where('merchant_id', '=', $plan['merchant_id'])->find();
            $mWithdraw->income_amount = bcadd($mWithdraw->income_amount, $amount, 2);
            $mWithdraw->withdrawal_amount = bcadd($mWithdraw->withdrawal_amount, $amount, 2);
            $mWithdraw->save();
            Db::commit();
            echo "success"; exit();
        }catch (\Exception $e){
            Db::rollback();
            //Log::info($e->getMessage() . '。第' . $e->getLine() . '行');
            echo "fail"; exit();
        }
    }
}